<template>
	<view>
		<view class="search-box">
			<uni-search-bar :focus="true" :bgColor="bgColor" cancelButton="none" :radius="100" @confirm="search"
				@input="input"></uni-search-bar>
		</view>
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" @click="gotoDetail(item.goods_id)" v-for="(item,i) in searchResults" :key="i">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		<!-- 历史记录 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons @click="clean" type="trash" size="17"></uni-icons>
			</view>
			<view class="history-list" >
				<uni-tag @click="gotoGoodsList(item)" :text="item" v-for="(item,i) in histories" :key="i"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 搜索框背景颜色
				bgColor: "#ffffff",
				// 防抖
				timer: null,
				// 关键词
				kw: '',
				// 搜索结果列表
				searchResults: [],
				// 历史记录
				historyList: ['a','apple', 'app'],
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(e) {
				// 防抖
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchResults()
					this.saveSearchHistory()
				}, 500)
			},
			//获取搜索数据
			async getSearchResults() {
				// 判断关键词为空，清空搜索结果
				if (this.kw === '') {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				// console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message

			},
			// 跳转页面
			gotoDetail(goodsId){
			// console.log(goodsId)
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id=" + goodsId
				})
			},
			// 添加关键词
			saveSearchHistory(){
				// new 一个set实例对象 声明一个常量接收 将历史记录传为默认
				const set = new Set(this.historyList)
				//添加之前先删除该关键字
				set.delete(this.kw)
				//追加关键字
				set.add(this.kw)
				// 将set对象转化为数组赋值给历史记录
				this.historyList = Array.from(set)
				console.log(set)
				// this.historyList.push(this.kw) 
				// this.historyList.unshift(this.kw); 
				// this.historyList = [...new Set(this.historyList)]
				// 对搜索过的数据进行持久化的存储
				// 犯了常识错误 对api不熟练 set 是设置 get是获取
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 清除历史记录 清楚storage本地存储
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			// 点击历史记录跳转商品页面
			gotoGoodsList(kw){
				console.log(kw)
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+ kw
				})
			}
		},
		computed:{
			// 关键词顺序处理
			histories(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		background-color: #c00000;
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.sugg-list {
		padding:  0 5px;
		
		.sugg-item {
			
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			
			.goods-name {
				// 当行显示
				white-space: nowrap;
				// 超出隐藏
				overflow: hidden;
				// 超出点点点 ... 表示
				text-overflow: ellipsis;
			}
		}
	}
	.history-box{
		padding: 0 5px;
		.history-title{
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}
		.history-list{
			display: flex;
			flex-wrap: wrap;
			.uni-tag {
				display: block;
				margin-top:5px;
				margin-right: 5px;
				background-color: #efefef;
				border: none;
				color: #000;
			}
		}
	}
</style>
new Set(thi